package com.fufu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fufu.common.RedisCache;
import com.fufu.pojo.FileCountNumber;
import com.fufu.service.FileCountNumberService;
import com.fufu.mapper.FileCountNumberMapper;
import com.fufu.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author chuwencang
* @description 针对表【file_count_number】的数据库操作Service实现
* @createDate 2025-05-20 16:25:35
*/
@Service
public class FileCountNumberServiceImpl extends ServiceImpl<FileCountNumberMapper, FileCountNumber>
implements FileCountNumberService{
    @Autowired
    private RedisCache<FileCountNumber> redisCache;

    @Autowired
    private FileCountNumberMapper mapper;


    @Override
    public Integer fileCountNumbers(String directoryPath) {
        File directory = new File(directoryPath);
        if (!directory.exists()||!directory.isDirectory()) {
            throw new IllegalArgumentException("您提供的目录无效或不存在这个目录");
        }
        int count = 0;
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    String fileName = file.getName().toLowerCase();
                    if (fileName.endsWith("doc")||fileName.endsWith("docx")||fileName.endsWith("xlsx")||fileName.endsWith("xls")) {
                        count++;
                    }
                }
            }
        }
        //保存统计数据
        FileCountNumber fileCountNumber = new FileCountNumber(directoryPath,1,null,null,new Date(),"1","doc;docx;xlsx;xls",count);
        this.saveOrUpdate(fileCountNumber);
        //跟新缓存
        this.selectFileCountNumberList();
        return count;
    }

    @Override
    public Page<FileCountNumber> selectFileCountNumber(int pageNum, int pageSize) {
        Page<FileCountNumber> page=new Page<>(pageNum,pageSize);
        List<FileCountNumber> fileCountNumber =  redisCache.getList("FileCountNumber");
        if (fileCountNumber == null||fileCountNumber.size()==0) {
            LambdaQueryWrapper<FileCountNumber> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.orderByDesc(FileCountNumber::getId);
            Page<FileCountNumber> list = this.page(page, queryWrapper);
            this.selectFileCountNumberList();
            return list;
        }else {
            //手动分页
            List<FileCountNumber> l = PageUtils.getPage(fileCountNumber, pageNum, pageSize);
            page.setRecords(l);
            page.setTotal(fileCountNumber.size());
            return page;
        }
    }

    @Override
    public Boolean saveFileCountNumber(FileCountNumber fileCountNumber) {
        boolean save = this.save(fileCountNumber);
        this.selectFileCountNumberList();
        return save;
    }

    @Override
    public Boolean updateFileCountNumber(FileCountNumber fileCountNumber) {
        boolean update = this.updateById(fileCountNumber);
        this.selectFileCountNumberList();
        return update;
    }

    @Override
    public Boolean deleteFileCountNumber(Long id) {
        boolean delete = this.removeById(id);
        this.selectFileCountNumberList();
        return delete;
    }

    @Override
    public FileCountNumber selectFileCountNumberById(Long id) {
        return getById(id);
    }

    @Override
    public List<FileCountNumber> selectFileCountNumberList() {
        List<FileCountNumber> fileCountNumbers = mapper.selectFileCountNumber();
        redisCache.deleteList("FileCountNumber");
        redisCache.saveList("FileCountNumber",fileCountNumbers);
        return fileCountNumbers;
    }


}
